package com.sojson.util.security.shiro.realm;

import javax.servlet.ServletRequest;

import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.subject.PrincipalCollection;

import com.sojson.util.cache.CacheUtil;
import com.sojson.util.servlet.ServletUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 自定义AuthorizingRealm,重写的Realm的通用方法(可以有多个Realm)
 * 
 * @author liu
 * @date 2020-08-08
 */
@Slf4j
public abstract class AbstractCustomAuthorizingRealm extends AbstractCustomPrivateAuthorizingRealm {

    // =======================================================================================================
    /** 新增的:往请求作用域中存储缓存 */
    private final String redisShiroCache = CacheUtil.getKeyBase("request");
    // =======================================================================================================

    /**
     * 重写的方法
     */
    @Override
    protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {

        if (principals == null) {
            return null;
        }
        // =======================================================================================================
        // TODO 新增全部
        ServletRequest servletRequest = ServletUtil.getServletRequest();
        Object attribute = servletRequest.getAttribute(redisShiroCache);
        if (attribute != null) {
            return (AuthorizationInfo)attribute;
        }
        // =======================================================================================================
        AuthorizationInfo info = null;

        if (log.isTraceEnabled()) {
            log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");
        }

        Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
        if (cache != null) {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to retrieve the AuthorizationInfo from cache.");
            }
            Object key = getAuthorizationCacheKey(principals);
            info = cache.get(key);
            if (log.isTraceEnabled()) {
                if (info == null) {
                    log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
                } else {
                    log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
                }
            }
        }

        if (info == null) {
            // Call template method if the info was not found in a cache
            info = doGetAuthorizationInfo(principals);
            // If the info is not null and the cache has been created, then cache the
            // authorization info.
            if (info != null && cache != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Caching authorization info for principals: [" + principals + "].");
                }
                Object key = getAuthorizationCacheKey(principals);
                cache.put(key, info);
            }
        }
        // =======================================================================================================
        // TODO 新增全部
        servletRequest.setAttribute(redisShiroCache, info);
        // =======================================================================================================

        return info;
    }

}